//定义放映厅相关的接口
const express = require('express')
const router = express.Router()
const response = require('../pool/Response')
//引入mysql连接池
const pool = require('../pool/pool')
//引入joi模块
const joi = require('joi')

/**
 * 查询放映厅类型
 */
 router.get('/cinema/room-list-type',(req,res) => {
    pool.query('select * from movie_cinema_room_type',(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error;
        }
        res.send(response.ok(result))
    })
})
//添加放映厅
router.post('/cinema-room/add',(req,res) => {
    let {room_name,room_type,movie_cinema_id} = req.body
    let schema = joi.object({
        room_name:joi.string().required(),
        room_type:joi.string().required(),
        movie_cinema_id:joi.string().required(),
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('insert into movie_cinema_room (room_name,room_type,movie_cinema_id) values(?,?,?)',[room_name,room_type,movie_cinema_id],(error,result)=>{
        if(error){
            res.send(response.error(500,error))
        }
        res.send(response.ok(result))
    })
})
//查询影院所有放映厅列表
router.get('/cinema-rooms/cinemaid',(req,res) => {
    let {cinema_id} = req.query
    let schema = joi.object({
        cinema_id:joi.string().required(),
    })
    let {error,value} = schema.validate(req.query)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('select * from movie_cinema_room where movie_cinema_id = ?',[cinema_id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error;
        }
        res.send(response.ok(result))
    })
})
//删除放映厅
router.post('/cinema-room/del',(req,res) => {
    let {id} = req.body
    let schema = joi.object({
        id:joi.string().required(),
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('delete from movie_cinema_room where id = ?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok(result))
    })
})
router.post('/cinema-room/edit-seat-template',(req,res) => {
    let {id,seat_template,room_size} = req.body
    let schema = joi.object({
        id:joi.string().required(),
        seat_template:joi.string().required(),
        room_size:joi.string().required(),
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    let sql = 'update movie_cinema_room set seat_template=?,room_size=? where id=?'
    pool.query(sql,[seat_template,room_size,id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok())
    })
})
//通过ID查询放映厅
router.get('/cinema-room/query',(req,res) => {
    let {id} = req.query
    //表单验证
    let schema = joi.object({
        id:joi.string().required() //必填
    })
    let {error,value} = schema.validate(req.query)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query(`select 
    mcr.room_name cinema_room_name,
    mcr.room_size cinema_room_size,
    mcr.movie_cinema_id cinema_id,
    mcr.room_type cinema_room_type,
    mc.cinema_name cinema_name
     from movie_cinema_room mcr join movie_cinema mc on mcr.movie_cinema_id=mc.id
     where mcr.id =?`,[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok(result[0]))
    })
})
module.exports = router